/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import static Dao.Conexao.getConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entity.Marca;

/**
 *
 * @author helton
 */
public class MarcaDao extends Conexao {

    public static Marca insert(Marca marca) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Insert Into marca (descmarca)"
                    + "Values(?)");
            pstmt.setObject(1, marca.getDescMarca());
            pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return marca;
    }

    public static Marca update(Marca marca) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Update marca Set descmarca = ?"
                    + "Where idmarca = ?");
            pstmt.setObject(1, marca.getDescMarca());
            pstmt.setObject(2, marca.getIdMarca());
            pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            System.out.println("Erro ao alterar marca: "+e);
        }
        return marca;
    }

    public static boolean remove(Marca marca) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Delete from marca Where idmarca =?");
            pstmt.setObject(1, marca.getIdMarca());
            pstmt.executeUpdate();
            //
            pstmt.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Marca find(int idmarca) {
        Marca marca = null;
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Select * from marca Where idmarca =?");
            pstmt.setObject(1,idmarca);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                marca = new Marca();
                marca.setIdMarca(rs.getInt("idmarca"));
                marca.setDescMarca(rs.getString("descmarca"));
            }
            //pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return marca;
    }

    public static List<Marca> getAll() {
        List<Marca> listMarca = new ArrayList<Marca>();
        Marca marca = null;
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Select * from marca");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                marca = new Marca();
                marca.setIdMarca(rs.getInt("idmarca"));
                marca.setDescMarca(rs.getString("descmarca"));
                listMarca.add(marca);
            }
            //pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listMarca;
    }
}
